package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.cacheable.Cacheable;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.exceptions.BusinessException;


import com.itheima.pojo.OrderSettingList;
import com.itheima.pojo.Setmeal;
import com.itheima.service.OrderSettingListService;


import com.itheima.service.SetMealService;
import org.apache.ibatis.annotations.Param;
import org.springframework.validation.Errors;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("ordersettinglist")
public class OrederSettingListController {
    @Reference(version = "1.0.0")
    private OrderSettingListService orderSettingListService;
    @Reference(version = "1.0.0")
    private SetMealService setMealService;
    @RequestMapping("add")
    public Result add(@RequestBody OrderSettingList orderSettingList,@Param("name")String name, Errors errors) {
        System.out.println(name);
        if (errors.hasErrors()) {
            StringBuilder msg = new StringBuilder();
            List<FieldError> fieldErrors = errors.getFieldErrors();
            for (int i = 0; i < fieldErrors.size(); i++) {
                if (i != fieldErrors.size() - 1) {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append(",");
                } else {
                    msg.append(fieldErrors.get(i).getDefaultMessage()).append("!");
                }
            }
            return new Result(false, msg.toString());
        }
        orderSettingListService.add(orderSettingList,name);
        return new Result(true, MessageConstant.ADD_ORDERSETTINGLIST_SUCCESS);
    }

    @RequestMapping("update")
    public Result update(@RequestBody OrderSettingList orderSettingList) {

        orderSettingListService.update(orderSettingList);
        return new Result(true, MessageConstant.EDIT_ORDERSETTINGLIST_SUCCESS);
    }

    @RequestMapping("delete")
    public Result delete(@Param("id") Integer id, @Param("date") Date date) {
        orderSettingListService.delete(id,date);
        return new Result(true, MessageConstant.DELETE_ORDERSETTINGLIST_SUCCESS);
    }

    @RequestMapping("findById/{id}")
    public Result findById(@PathVariable int id) {
        OrderSettingList orderSettingList = orderSettingListService.findById(id);
        return new Result(true, MessageConstant.QUERY_ORDERSETTINGLIST_SUCCESS, orderSettingList);
    }

    @RequestMapping("updateReservation")
    public Result updateReservation(Date date){
        orderSettingListService.updateReservation(date);
        return new Result(true,MessageConstant.UPDATERESERVATION_SUCCESS);
    }
    @Cacheable(expire = 600)
    @RequestMapping("findAll")
    public Result findAll() {
        List<OrderSettingList> list = orderSettingListService.findAll();
        if (list == null || list.size() <= 0) {
            throw new BusinessException("查询失败，请联系管理员");
        }
        System.out.println("这里是查询所有预约信息，我从数据库查询了！");
        return new Result(true, MessageConstant.QUERY_ORDERSETTINGLIST_SUCCESS, list);
    }

    @RequestMapping("findPage")
    public PageResult findPage(@RequestBody QueryPageBean queryPageBean) {
        PageResult pageResult = orderSettingListService.findPage(
                queryPageBean.getCurrentPage(),
                queryPageBean.getPageSize(),
                queryPageBean.getQueryString()
        );
        return pageResult;
    }
}
